package co.fitstart.admin.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.entity.user.agent.RecordType;

@Dynamic("hibernate")
public interface AgentDao {
    
    @Limit
    @Query("FROM Agent WHERE disabled = false")
    public List<Agent> listAgent(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @return
     */
    @Query("FROM Agent WHERE disabled = false AND purchaserTotal != 0")
    public List<Agent> listAgent();
    
    /**
     * 
     * @param userId
     * @return
     */
    @Query("FROM Agent WHERE disabled = false AND user.id = :userId")
    public Agent loadAgent(@Variable("userId") Long userId);
    
    /**
     * 
     * @param agent
     */
    @Execute("UPDATE AgentDiscount SET disabled = true WHERE agent = :agent")
    public void deleteAgentDiscount(@Variable("agent") Agent agent);
    
    /**
     * 
     * @param agent
     * @return
     */
    @Query("FROM AgentDiscount WHERE disabled = false AND agent = :agent")
    public List<AgentDiscount> listAgentDiscount(@Variable("agent") Agent agent);
    
    /**
     * 
     * @param code
     * @return
     */
    @Query("FROM AgentDiscount WHERE discountCode = :code")
    public AgentDiscount loadAgentDiscount(@Variable("code") String code);
    
    /**
     * 
     * @param agent
     * @param types
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM AgentRecord WHERE disabled = false AND agent = :agent AND recordType in (:type) ORDER BY recordType, occurTime DESC")
    public List<AgentRecord> listAgentRecord(@Variable("agent") Agent agent, @Variable("type") RecordType[] types, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param recordType
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM AgentRecord WHERE disabled = false AND recordType = :recordType ORDER BY occurTime DESC")
    public List<AgentRecord> listAgentRecord(@Variable("recordType") RecordType recordType, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param recordType
     * @param beginTime
     * @param endTime
     * @return
     */
    @Query("FROM AgentRecord WHERE disabled = false AND recordType = :recordType AND occurTime BETWEEN :beginTime AND :endTime ORDER BY occurTime DESC")
    public List<AgentRecord> listWithdrawingAgentRecord(@Variable("recordType") RecordType recordType, @Variable("beginTime") Date beginTime, @Variable("endTime") Date endTime);
    
    /**
     * 
     * @param recordType
     * @param beginTime
     * @param endTime
     * @return
     */
    @Query("FROM AgentRecord WHERE disabled = false AND recordType = :recordType AND payTime BETWEEN :beginTime AND :endTime ORDER BY occurTime DESC")
    public List<AgentRecord> listWithdrawedAgentRecord(@Variable("recordType") RecordType recordType, @Variable("beginTime") Date beginTime, @Variable("endTime") Date endTime);
}
